package com.li.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.li.domain.*;
import com.li.mapper.*;
import com.li.service.SystemAddressService;
import com.li.utlis.FilePathUtil;
import com.li.utlis.FileUploadUtil;
import com.li.utlis.MenuUtil;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.li.service.AdminService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService{

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    MenuMapper menuMapper;

    @Autowired
    AdminRoleMapper adminRoleMapper;

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    UploadFileMapper uploadFileMapper;

    @Autowired
    SystemAddressService systemAddressService;

    @Override
    public Admin login(String username, String password) {
        Admin admin = adminMapper.login(username, password);
        return admin;
    }

    @Override
    public List<Admin> listPage(Admin admin, Integer page, Integer limit) {
        Integer indexpage=(page-1)*limit;
        if (admin==null){
            admin=new Admin();
        }
        Integer deptId = admin.getDeptId();
        if (deptId!=null && !"".equals(deptId)){
            admin.setDeptIdsInt(deptMapper.getdeptIdsById(deptId));
        }
        List<Admin> admins = adminMapper.listPage(admin, indexpage, limit);
        for (Admin a:admins){
            Admin adminRoleNameById = adminMapper.getAdminRoleNameById(a.getId());
            a.setRoleName(adminRoleNameById.getRoleName());
            //String[] rIds = adminRoleNameById.getRoleIds().split(",");
            a.setRoleIds(adminRoleNameById.getRoleIds());
        }
        return admins;
    }

    @Override
    public Integer listPageCount(Admin admin) {
        if (admin==null){
            admin=new Admin();
        }
        return adminMapper.listPageCount(admin);
    }

    /**
     * 修改管理员，在修改时可能会添加角色
     * @param admin
     */
    @Transactional
    @Override
    public void updateAdmin(Admin admin) {
        //先删除
        Integer aId=admin.getId();
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().eq("admin_id",aId));
        //如果存在角色则添加
        if (admin.getRoleIdsInt()!=null && admin.getRoleIdsInt().length>0){
            for (Integer rId:admin.getRoleIdsInt()){
                AdminRole adminRole=new AdminRole();
                adminRole.setAdminId(aId);
                adminRole.setRoleId(rId);
                adminRoleMapper.insert(adminRole);
            }
        }
        //修改管理员
        adminMapper.updateById(admin);
    }

    @Override
    public void addAdmin(Admin admin) {
        admin.setCreateTime(new Date());
        adminMapper.insert(admin);
    }

    /**
     * 删除管理员和他的角色
     * @param admin
     */
    @Transactional
    @Override
    public void delAdmin(Admin admin) {
        //删除中间表的数据
        adminRoleMapper.delete(new QueryWrapper<AdminRole>().eq("admin_id",admin.getId()));
        //删除本身的数据
        adminMapper.deleteById(admin);
    }

    /**
     * 上传头像
     * @param uploadFile
     * @param req
     * @return
     */
    @Transactional
    @Override
    public String uploadAvatar(MultipartFile uploadFile, HttpServletRequest req) {
        /*
        上传文件 并 将数据上传文件记录表
         */
        //找到要上传的地址
        String path= FilePathUtil.getSystemAddressServicePath(systemAddressService, "admin-avatar");
        //完善地址 加"/"
        path= FilePathUtil.pathLast(path);
        //把文件上传
        UploadFile fileData = FileUploadUtil.uploadPahtFileAndUploadFile(uploadFile, path, req);
        //添加到文件记录表中
        uploadFileMapper.insert(fileData);
        //将访问地址返回
        return fileData.getPath();
    }

    @Override
    public Admin editAdmin(Integer id) {
        return adminMapper.editAdmin(id);
    }
}
